package controllers

import akka.actor.ActorSystem
import javax.inject._
import play.api.Logging
import play.api.libs.json.Json
import play.api.mvc._
import scala.concurrent.ExecutionContext

/**
 * This controller creates an `Action` that demonstrates how to write
 * simple asynchronous code in a controller. It uses a timer to
 * asynchronously delay sending a response for 1 second.
 *
 * @param cc          standard controller components
 * @param actorSystem We need the `ActorSystem`'s `Scheduler` to
 *                    run code after a delay.
 * @param exec        We need an `ExecutionContext` to execute our
 *                    asynchronous code.  When rendering content, you should use Play's
 *                    default execution context, which is dependency injected.  If you are
 *                    using blocking operations, such as database or network access, then you should
 *                    use a different custom execution context that has a thread pool configured for
 *                    a blocking API.
 */
@Singleton
class TestController @Inject()(
                                cc: ControllerComponents,
                                actorSystem: ActorSystem
                              )
                              (implicit exec: ExecutionContext, parser: BodyParsers.Default)
  extends AbstractController(cc) with Logging {
  implicit val myCustomCharset = Codec.javaSupported("gb18030")

  def index = checkLogin(parser, exec) {
    Ok("errorInfo") //.as("text/html;charset=gb18030")
  }

  def show = Action { implicit request =>
    Ok("dd.sameCodeUpdate()")
  }

  def test = Action { implicit request =>

    Ok("test")
  }

  def post = Action { implicit request =>
    val data = request.body.asJson.get
    logger.debug(data.toString())
    // {"accid":"32424234","formid":"mo","op":"delete","order":"DJS0001"}
    val formid = (data \ "formid").as[String]
    val op = (data \ "op").as[String]
    val order = (data \ "order").as[String]
    val res = Json.obj(
      "formid"-> data("formid"),
      "order"->data("order"),
      "op"->data("op"),
      "result"->"{\"result\":true,\"msg\":\"更新成功\",\"content\":[\"TRUE: 第1条更新成功\"]}"
    )
    Ok(res)

  }

}
